#include <string_view>

#include "ExtendDeviceManager10.h"
#include "GuiContext.h"
#include "CodeScanner.h"
#include "PowerMonitor.h"
#include "DoorAlertor.h"
#include "AcSwitchDetector.h"
#include "ConfigManager.h"
#include "CurrentVoltageManager.h"


ExtendDeviceManager10::ExtendDeviceManager10() : IExtendDeviceManager(mouterCommunicator) {
}

ExtendDeviceManager10::~ExtendDeviceManager10() {
    //dtor
}



void ExtendDeviceManager10::initialize() {
    DeviceChannel::initSignalEnv();


    IExtendDeviceManager::initialize();

}


bool ExtendDeviceManager10::updateOnlineDevice(ChannelDevices& channelDevices) {
    DummyChannel& channel = *channelDevices.first;

    std::forward_list<ExtendDevicePtr>& deviceList =  channelDevices.second;
    bool allDeviceOnline = true;
    bool deviceOnline;
    //  std::forward_list<ExtendDevicePtr>::iterator iterator =deviceList.begin();

    if (deviceList.empty()) {
        allDeviceOnline = false;
    } else {
        deviceList.remove_if([&allDeviceOnline, &deviceOnline, &channel](ExtendDevicePtr& pExtendDevicePtr) {
            deviceOnline = pExtendDevicePtr->recheck();
            LOG_DEDUG("%s,recheck:%d", channel.mexceptionTip.c_str(), deviceOnline);
//        if (deviceOnline){
//            ++allDeviceOnline;
//        }
            allDeviceOnline &= deviceOnline;
            return !deviceOnline;

        });

    }

    return allDeviceOnline;
}




bool ExtendDeviceManager10::doWork() {
    std::string prefix;
    DeviceChannel::waitAndRead(prefix, mouterCommunicator, mouterCommunicator.mbuffer, sizeof(mouterCommunicator.mbuffer));

    return true;

    mnewOnlineDevice.clear();
//   ExtendDevice* device;
    ChannelDevices* pChannelDevices = channelDevicesArray;
    bool allDeviceOnline = true;
    for (int i = 1; i < DEFAULT_CHANNEL_COUNT; ++i) {//
        pChannelDevices = channelDevicesArray + i;

        allDeviceOnline = updateOnlineDevice(*pChannelDevices);

        if (!allDeviceOnline) {
            findExtendDevice(*pChannelDevices);
        }
    }
    Task* pTask = &mrefreshTask;
    mpool->pushPool(pTask);

    usleep(2400*1000);
    return true;
}


